﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Steema.TeeChart;
using AAA.Meta.Chart.Data;

namespace AAA.TeeChart
{
    public partial class SignalInfoPane : UserControl
    {
        private Steema.TeeChart.Axis _custAxisBottom;
        private Steema.TeeChart.Axis _custAxisLeft;
        private Steema.TeeChart.Axis _custAxisRight;
        private Steema.TeeChart.Styles.Bar _barSeries;
        private Dictionary<string, Color> _dicLightMapping; // Signal <=> Color
        private BarDataExtra _barDataExtra;
        private List<string> _lstTitle;
        private Dictionary<string, string> _dicTitleMapping; //  Title <==> Column
        private List<string> _lstActiveTitle;
        private string _strDisplayKey;

        private float _fXMin;
        private float _fXMax;

        public SignalInfoPane()
        {
            InitializeComponent();
            _dicLightMapping = new Dictionary<string, Color>(); 
            _dicTitleMapping = new Dictionary<string, string>();
            _lstActiveTitle = new List<string>();
            _lstTitle = new List<string>();
            FormatChart();
        }

        public float XMin
        {
            get { return _fXMin; }
            set { _fXMin = value; }
        }

        public float XMax
        {
            get { return _fXMax; }
            set { _fXMax = value; }
        }

        public void ResetXRange()
        {
            _custAxisBottom.SetMinMax(XMin, XMax);
        }

        public string DisplayKey
        {
            get { return _strDisplayKey; }
            set { _strDisplayKey = value; }
        }

        public BarDataExtra BarDataExtra
        {
            get { return _barDataExtra; }
            set { _barDataExtra = value; }
        }

        public void AddActiveTitle(string[] strTitles)
        {
            try
            {
                foreach (string strTitle in strTitles)
                    AddActiveTitle(strTitle);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "," + ex.StackTrace);
            }
        }

        public void AddActiveTitle(string strTitle)
        {
            try
            {
                if(_lstActiveTitle.IndexOf(strTitle) > -1)
                    return;
                _lstActiveTitle.Add(strTitle);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "," + ex.StackTrace);
            }
        }

        public void AddTitleMapping(string strTitle, string strColumnName)
        {
            if (_dicTitleMapping.ContainsKey(strTitle))
                _dicTitleMapping[strTitle] = strColumnName;
            else
            {
                _dicTitleMapping.Add(strTitle, strColumnName);
                _lstTitle.Add(strTitle);
            }
        }

        public void AddColorMapping(string strLabel, Color cColor)
        {
            if (_dicLightMapping.ContainsKey(strLabel))
                _dicLightMapping[strLabel] = cColor;
            else
                _dicLightMapping.Add(strLabel, cColor);
        }

        public void Display()
        {
            object oInfo;

            try
            {                
                chtChart.Series.RemoveAllSeries();
                                   
                for (int i = 0; i < _lstActiveTitle.Count; i++)
                {
                    _barSeries = new Steema.TeeChart.Styles.Bar();
                    _barSeries.BarWidthPercent = 100;
                    _barSeries.CustomHorizAxis = _custAxisBottom;
                    _barSeries.CustomVertAxis = _custAxisRight;
                    _barSeries.MultiBar = Steema.TeeChart.Styles.MultiBars.None;       
                    for (int j = 0; j < _barDataExtra.GetRecordCount(); j++)
                    {
                        oInfo = _barDataExtra.GetInfo(_dicTitleMapping[_lstActiveTitle[i]], j);
                        if (oInfo == null)
                            continue;
                        _barSeries.Add(j, 1, _dicLightMapping.ContainsKey(oInfo.ToString()) ? _dicLightMapping[oInfo.ToString()] : Color.White);
                    }
                    chtChart.Series.Add(_barSeries);
                    _barSeries.MultiBar = Steema.TeeChart.Styles.MultiBars.Stacked;
                    _barSeries.Marks.Visible = false;
                    _barSeries.Marks.Style = Steema.TeeChart.Styles.MarksStyles.Value;
                    
                    _barSeries.ColorEach = true;                    
                }
                                
                
/*
                for (int i = 0; i < _barDataExtra.GetRecordCount(); i++)
                {
                    for (int j = 0; j < _lstActiveTitle.Count; j++)
                    {
                        oInfo = _barDataExtra.GetInfo(_dicTitleMapping[_lstActiveTitle[j]], i);
                        _barSeries.Add(i, 1, _dicLightMapping.ContainsKey(oInfo.ToString()) ? _dicLightMapping[oInfo.ToString()] : Color.White);
                    }
                }
 */                 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "," + ex.StackTrace);
            }
        }

        private void FormatChart()
        {
            try
            {
                chtChart.Header.Text = "";
                chtChart.BackColor = Color.Black;
                chtChart.Aspect.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                chtChart.Aspect.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                chtChart.Aspect.View3D = false;
                chtChart.AutoRepaint = true;
                chtChart.Panel.MarginUnits = PanelMarginUnits.Pixels;
                chtChart.Panel.MarginBottom = 40;
                chtChart.Panel.MarginLeft = 5;
                chtChart.Panel.MarginRight = 100;
                chtChart.Legend.Visible = false;
                chtChart.Axes.Left.Automatic = true;
                chtChart.Axes.Right.Automatic = true;                

                chtChart.Axes.Top.Visible = false;
                chtChart.Axes.Bottom.Visible = false;
                chtChart.Axes.Left.Visible = false;
                chtChart.Axes.Right.Visible = false;

                _custAxisBottom = new Axis(true, false, chtChart.Chart);
                _custAxisLeft = new Axis(false, false, chtChart.Chart);
                _custAxisRight = new Axis(false, true, chtChart.Chart);

                chtChart.Axes.Custom.Add(_custAxisBottom);
                chtChart.Axes.Custom.Add(_custAxisLeft);
                chtChart.Axes.Custom.Add(_custAxisRight);

                _custAxisBottom.AxisPen.Color = System.Drawing.Color.Black;
                _custAxisBottom.AxisPen.Width = 1;
                _custAxisBottom.Grid.Color = System.Drawing.Color.FromArgb(128, 128, 128);
                _custAxisBottom.Grid.Transparency = 50;
                _custAxisBottom.Labels.Font.Brush.Color = System.Drawing.Color.Black;
                _custAxisBottom.MinorTickCount = 10;
                _custAxisBottom.PositionUnits = Steema.TeeChart.PositionUnits.Pixels;
                _custAxisBottom.Title.Visible = false;
                _custAxisBottom.Visible = false;

                _custAxisLeft.AxisPen.Color = System.Drawing.Color.Black;
                _custAxisLeft.AxisPen.Width = 1;
                _custAxisLeft.Grid.Color = System.Drawing.Color.FromArgb(128, 128, 128);
                _custAxisLeft.Grid.DrawEvery = 2;
                _custAxisLeft.Grid.Transparency = 50;
                _custAxisLeft.Labels.Font.Brush.Color = System.Drawing.Color.Black;
                _custAxisLeft.Labels.ValueFormat = "0.00";
                _custAxisLeft.MinorTickCount = 1;
                _custAxisLeft.PositionUnits = Steema.TeeChart.PositionUnits.Pixels;
                _custAxisLeft.Title.Visible = false;
                _custAxisLeft.Visible = false;

                _custAxisRight.AxisPen.Color = System.Drawing.Color.Black;
                _custAxisRight.AxisPen.Width = 1;
                _custAxisRight.Grid.Visible = false;
                _custAxisRight.Labels.Font.Brush.Color = System.Drawing.Color.Black;
                _custAxisRight.Labels.ValueFormat = "0.00";
                _custAxisRight.MinorTickCount = 1;
                _custAxisRight.PositionUnits = Steema.TeeChart.PositionUnits.Pixels;
                _custAxisRight.Title.Visible = false;
                _custAxisRight.Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "," + ex.StackTrace);
            }            
        }
    }
}
